var APIURL = 'http://rest.benxinkeji.cn'; //APIURL请求地址
var USERINFO = ''; //用户信息存储
var MEMBERID = localStorage.getItem('MEMBERID') ? localStorage.getItem('MEMBERID') : '';
var MEMBERTOKEN = localStorage.getItem('MEMBERTOKEN') ? localStorage.getItem('MEMBERTOKEN') : '';

function route(url, id, data) { //新页面跳转
    mui.openWindow({
        url: url,
        id: id,
        extras: data
    })
}

function getLocation(obj) { //测试地理位置经纬度
    localStorage.setItem('latitude', '46.57997');
    localStorage.setItem('longitude', '125.16513');
    // AMap.plugin('AMap.Geolocation', function() {
    //     var geolocation = new AMap.Geolocation();
    //     geolocation.getCurrentPosition(function(status, result) {
    //         console.log(status)
    //         if (status == 'complete') {
    //             obj(result.position)
    //             localStorage.setItem('latitude', result.position.lat);
    //             localStorage.setItem('longitude', result.position.lng);
    //         }
    //     });
    // });

}

function setItem(key, value) { //保存本地
    console.log(key, value)
    if (Object.prototype.toString.call(value) == '[object Object]') {
        localStorage.setItem(key, JSON.stringify(value))
    } else if (Object.prototype.toString.call(value) == '[object String]') {
        localStorage.setItem(key, value)
    }
}

function getItem(key) { //获取本地
    // if (Object.prototype.toString.call(localStorage.getItem(key)) == '[object Object]') {
    //     return localStorage.getItem(key)
    // } else if (Object.prototype.toString.call(localStorage.getItem(key)) == '[object String]') {
    return JSON.parse(localStorage.getItem(key))
        // }
}

jQuery.gets = function(url, type, data, obj) { //AJAXGET方法
    // url=接口地址
    // type=同步一步开关，false=同步，true=异步
    // data=数据来源，格式{参数名:参数值}
    var types = (type == null || type == "" || typeof(type) == "undefined") ? type : true
    var data = (data == null || data == "" || typeof(data) == "undefined") ? { "date": new Date().getTime() } : data;
    $.ajax({
        type: "get",
        headers: {
            memberId: MEMBERID,
            memberToken: MEMBERTOKEN
        },
        async: types,
        data: data,
        url: APIURL + url,
        dataType: "json",
        success: function(e) {
            obj(r)
        },
        error: function(e) {
            obj(e)
        }
    })
}

var versions = function() { //判断设备
    var u = navigator.userAgent,
        app = navigator.appVersion;
    return {
        trident: u.indexOf('Trident') > -1, //IE内核
        presto: u.indexOf('Presto') > -1, //opera内核
        webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
        gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
        mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
        ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
        android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
        iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
        iPad: u.indexOf('iPad') > -1, //是否iPad
        webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
        weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
        qq: u.match(/\sQQ/i) == " qq" //是否QQ
    };
}()


function post(url, data, obj) { //AJAXPOST方法

    // var types = (type != null || type != "" || typeof(type) != "undefined") ? type : true
    u = navigator.userAgent
    console.log(u)
    var data = (data == null || data == "" || typeof(data) == "undefined") ? { "date": new Date().getTime() } : data;
    if (MEMBERID || MEMBERTOKEN) {
        var header = {
            "Accept": "application/json; charset=utf-8",
            "memberId": MEMBERID,
            "memberToken": MEMBERTOKEN
        }
    } else {
        var header = {
            "Accept": "application/json; charset=utf-8",
        }
    }
    console.log(header)
    if (!!u.match(/AppleWebKit.*Mobile.*/)) {
        $.ajax({
            type: "POST",
            headers: header,
            data: JSON.stringify(data),
            url: APIURL + url,
            dataType: "json",
            success: function(r) {
                console.log(r)
                if (r.errCode == 0) {
                    obj(r)
                    if (r.memberInfo) {
                        localStorage.setItem('MEMBERID', r.memberInfo.memberId)
                        localStorage.setItem('MEMBERTOKEN', r.memberInfo.memberToken)
                    }
                } else {
                    alert(r.errMsg)
                }
            },
            error: function(e) {
                obj(e)
            }
        })
    } else {
        console.log("不能访问")
    }
}